package com.h3bpm.kumhosunny.wcp.controller;

import OThinker.Common.Organization.Models.User;
import OThinker.H3.Controller.ControllerBase;
import com.h3bpm.kumhosunny.wcp.api.RestFulApi;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
/**
 * @Auther: Administrator
 * @Date: 2019/11/28 17:46
 * @Description:
 */

@Controller
@RequestMapping("/Portal/WcpConnController")
public class WcpConnController extends ControllerBase {

    private String erroMsg=null;

    @Override
    public String getFunctionCode() {
        return null;
    }

    //http://localhost:8081/Portal/WcpConnController/loginWcp

    @RequestMapping("/loginWcp")
    public void loginWcpByName(HttpServletResponse response)  throws IOException {
        Map<String,String> mapData=new HashMap<>(3);
        Map<String,String> resultData=new HashMap<>(3);

        //设置response编码
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        //获取当前登陆用户
        User user=null;
        try {
            user=getUserValidator().getUser();

        }catch (NullPointerException e){
            response.getWriter().println("<script>alert('当前未登陆状态请登陆重试!')</script>");
            return;
        }

        if(user!=null && user.getCode()!=null){

            //登陆之前判断知识库系统是否存在当前用户
            if("success".equals(RestFulApi.searchUser(user.getCode()))){
                 resultData=RestFulApi.signNoPass(user.getCode());

                 if(!"".equals(resultData.get("url"))){

                     response.sendRedirect(resultData.get("url")+resultData.get("certificate"));
                 }

            }else{
                response.getWriter().println("<script>alert('当前账户不存在知识库系统!')</script>");
                return;
            }



        }else{
            response.getWriter().println("<script>alert('当前账户信息异常!')</script>");
            return;
        }

    }
}
